﻿@page "/Centre/PersonalData"
@using iOSClub.Data
@using iOSClub.Data.DataModels
@using iOSClub.WebSite.Models
@using Microsoft.EntityFrameworkCore
@inject IDbContextFactory<iOSContext> DbFactory
@inject NavigationManager Nav

<PageTitle>我的信息 - 西建大iOS Club</PageTitle>

<PageView Style="height: 100%"
          FullScreen="true"
          OnDelete="@(() => Nav.NavigateTo("/Centre"))"
          Title="我的信息">
    <Extra>
        <p>当前身份:@MemberModel.IdentityDictionary[Member.Identity]</p>
    </Extra>
    <Context>
        <Form Model="Member"
              ValidateMode="@FormValidateMode.Rules"
              RequiredMark="FormRequiredMark.Optional"
              OnFinish="OnFinish"
              OnFinishFailed="OnFinishFailed">
            <FormItem Class="item" Label="姓名" Rules="@(SignRecord.UserNameRules)">
                <Input @bind-Value="@context.UserName"/>
            </FormItem>
            <FormItem Class="item" Label="性别" Rules="SignRecord.GenderRules">
                <RadioGroup @bind-Value="@context.Gender" ButtonStyle="@RadioButtonStyle.Solid">
                    @foreach (var item in SignRecord.Genders)
                    {
                        <Radio RadioButton Value="@item">@item</Radio>
                    }
                </RadioGroup>
            </FormItem>
            <FormItem Class="item" Label="学号" Rules="SignRecord.IdRules">
                <Input @bind-Value="@context.UserId">
            </FormItem>
            <FormItem Class="item" Label="学院" Rules="SignRecord.AcademyRules">
                <SimpleSelect @bind-Value="@context.Academy">
                    <SelectOptions>
                        @foreach (var item in SignRecord.Academies)
                        {
                            <SimpleSelectOption Value="@item" Label="@item"/>
                        }
                    </SelectOptions>
                </SimpleSelect>
            </FormItem>
            <FormItem Class="item" Label="政治面貌" Rules="SignRecord.PoliticalLandscapeRules">
                <RadioGroup @bind-Value="@context.PoliticalLandscape" ButtonStyle="@RadioButtonStyle.Solid">
                    @foreach (var item in SignRecord.PoliticalLandscapes)
                    {
                        <Radio RadioButton Value="@item">@item</Radio>
                    }
                </RadioGroup>
            </FormItem>

            <FormItem Class="item" Label="专业班级" Rules="SignRecord.ClassNameRules">
                <Input @bind-Value="@context.ClassName">
            </FormItem>

            <FormItem Class="item" Label="手机号码" Rules="SignRecord.PhoneNumRules">
                <Input @bind-Value="@context.PhoneNum">
            </FormItem>

            <FormItem Style="text-align: center">
                <Button Type="@ButtonType.Primary" HtmlType="submit">更改</Button>
            </FormItem>
        </Form>
        <br/>
    </Context>
</PageView>

<Modal Title="@("更改信息")"
       Visible="@_visible"
       OnOk="@HandleOk"
       OnCancel="@HandleCancel"
       OkText="@("确认")"
       CancelText="@("取消")"
       ConfirmLoading="@_confirmLoading">
    确定提交?
</Modal>

@code{
    bool _visible;
    bool _confirmLoading;

    private string Id { get; set; } = "";

    private async Task HandleOk(MouseEventArgs e)
    {
        _confirmLoading = true;
        StateHasChanged();
        await Done();
        _visible = false;
        _confirmLoading = false;
    }

    private void HandleCancel(MouseEventArgs e)
    {
        _visible = false;
    }

}

@code
{
    [CascadingParameter] private MemberModel Member { get; set; } = new();

    protected override void OnInitialized()
    {
        base.OnInitialized();
        Id = Member.UserId;
        if (Member.Identity != "Founder") return;
        Member.ClassName = "软件工程2019";
        Member.PhoneNum = "12345678901";
    }

    private void OnFinish()
    {
        _visible = true;
    }

    private async Task Done()
    {
        if (Loading)
            return;
        try
        {
            Loading = true;
            await using var context = await DbFactory.CreateDbContextAsync();
            if (Member.Identity == "Founder")
            {
                var founder = await context.Staffs.FirstOrDefaultAsync(x => x.UserId == Id);
                if (founder == null)
                {
                    return;
                }

                if (Id != Member.UserId)
                {
                    context.Staffs.Remove(founder);
                    context.Staffs.Add(new StaffModel() { Identity = founder.Identity, Name = Member.UserName, UserId = Member.UserId });
                }
                else
                {
                    founder.Name = Member.UserName;
                    founder.UserId = Member.UserId;
                }

                await context.SaveChangesAsync();
                return;
            }

            var stu = await context.Students.FirstOrDefaultAsync(x => x.UserId == Id);
            if (stu == null)
                context.Students.Add(Member);
            else
            {
                if (Id == Member.UserId)
                {
                    stu.UserName = Member.UserName;
                    stu.Academy = Member.Academy;
                    stu.ClassName = Member.ClassName;
                    stu.PhoneNum = Member.PhoneNum;
                    stu.Gender = Member.Gender;
                    stu.PoliticalLandscape = Member.PoliticalLandscape;
                }
                else
                {
                    context.Students.Remove(stu);
                    context.Students.Add(Member);
                    var staff = await context.Staffs.FirstOrDefaultAsync(x => x.UserId == Id);
                    if (staff != null)
                    {
                        context.Staffs.Remove(staff);
                        context.Staffs.Add(new StaffModel() { Identity = staff.Identity, Name = Member.UserName, UserId = Member.UserId });
                    }
                }
            }

            await context.SaveChangesAsync();
        }
        finally
        {
            Loading = false;
        }
    }

    private bool Loading { get; set; }

    private void OnFinishFailed()
    {
        Console.WriteLine("出问题了");
        Loading = false;
    }

}

<style>
    .item{
        padding-left: 10px;
        padding-right: 10px;
    }
    
    .form-control {
        display: block;
        width: 100%;
        height: 34px;
        padding: 6px 12px;
        font-size: 14px;
        line-height: 1.42857143;
        color: #555;
        background-color: #fff;
        background-image: none;
        border: 1px #ccc;
        border-radius: 4px;
        -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
        box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
        -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
        -o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
        transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
    }
   
   option{
        display: block;
        width: 100%;
        height: 34px;
        padding: 6px 12px;
        font-size: 14px;
        line-height: 1.42857143;
        color: #555;
   }
   
</style>